#!/bin/sh

boot=1
inp=c

DRV_DIR=/usr/lib/xorg/modules/input/
DRV_DIR64=/usr/lib64/xorg/modules/input/

if [ ! -f /opt/.touch_panel/uin ] && [ ! -f /opt/.touch_panel/nuin ]; then
	if [ -f $DRV_DIR/risintech_drv.so ] || [ -f $DRV_DIR64/risintech_drv.so ] || [ -f $DRV_DIR/xfhiddrv_drv.so ] || [ -f $DRV_DIR64/xfhiddrv_drv.so ]; then
		echo "Removing this driver will automatically"
		echo "restart of the operating system,"
		echo "Do you want to continue?"
		read -p "(y/n): " inp
	fi
fi

if [ $inp = "n" ]; then
	echo "exit"
	exit 1
elif [ $inp = "c" ]; then
	boot=0
fi
	
BIN_DIR=/usr/local/bin

if [ -f $BIN_DIR/closedev ]; then
	echo "close proccess"
	$BIN_DIR/closedev
fi

if [ -f /lib/modules/$(uname -r)/modules.dep.old ]; then
	mv -f /lib/modules/$(uname -r)/modules.dep.old /lib/modules/$(uname -r)/modules.dep
fi
#============================================================
# delete udev rules

for udev_path in /etc/udev/rules.d/ /lib/udev/rules.d/
do
	for rules_name in 10-serialtouch.rules 99-hiddev.rules 99-touchscreen.rules 50-mouse.rules 99-mouse.rules 10-touch-wakup.rules 99-touch-wakup.rules 99-uinput.rules 99-cooltouch.rules
	do
		if [ -f $udev_path/$rules_name ]; then
			rm -f $udev_path/$rules_name
		fi
	done
done
#============================================================

if [ -f /etc/idlist.conf ]; then
	rm -f /etc/idlist.conf
fi

if [ -f /etc/manuflist.conf ]; then
	rm -f /etc/manuflist.conf
fi

if [ -f /etc/rmanuflist.conf ]; then
	rm -f /etc/rmanuflist.conf
fi

if [ -f /etc/modprobe.d/touchmodp.conf ]; then
	rm -f /etc/modprobe.d/touchmodp.conf
fi

# delete binarie
rm -f /usr/bin/touchattach*


if [ -f /lib/modules/$(uname -r)/kernel/drivers/input/touchscreen/rtouch.ko ]; then
	rm -f /lib/modules/$(uname -r)/kernel/drivers/input/touchscreen/rtouch.ko
	/sbin/depmod
fi

if [ -f /etc/rc.local.old ]; then
	mv -f /etc/rc.local.old /etc/rc.local
fi

if [ -f /etc/rc.d/rc.local.old ]; then
	mv -f /etc/rc.d/rc.local.old /etc/rc.d/rc.local
fi

if [ -f /etc/init.d/after.local.old ]; then
	mv -f /etc/init.d/after.local.old /etc/init.d/after.local
fi

# restore 10-evdev.conf
if [ -f /etc/X11/xorg.conf.d/10-evdev.conf.old ]; then
	mv -f /etc/X11/xorg.conf.d/10-evdev.conf.old /etc/X11/xorg.conf.d/10-evdev.conf
fi

if [ -f /usr/share/X11/xorg.conf.d/10-evdev.conf.old ]; then
	mv -f /usr/share/X11/xorg.conf.d/10-evdev.conf.old /usr/share/X11/xorg.conf.d/10-evdev.conf
fi

if [ -f /usr/lib/X11/xorg.conf.d/05-evdev.conf.old ]; then
	mv -f /usr/lib/X11/xorg.conf.d/05-evdev.conf.old /usr/lib/X11/xorg.conf.d/05-evdev.conf
fi

input_fdi_path=/usr/share/hal/fdi/policy/10osvendor
if [ -f $input_fdi_path/10-x11-input.fdi.old ]; then
	mv -f $input_fdi_path/10-x11-input.fdi.old $input_fdi_path/10-x11-input.fdi
fi

input_fdi_path=/usr/share/hal/fdi/policy/10osvendor
if [ -f $input_fdi_path/10-x11-input.fdi.old ]; then
	mv -f $input_fdi_path/10-x11-input.fdi.old $input_fdi_path/10-x11-input.fdi
fi

synp_name=11-x11-synaptics.fdi
synp_fdi_path=/usr/share/hal/fdi/policy/10osvendor
if [ -f $synp_fdi_path/$synp_name.old ]; then
	mv -f $synp_fdi_path/$synp_name.old $synp_fdi_path/$synp_name
fi

synp_name=10-synaptics.fdi
synp_fdi_path=/usr/share/hal/fdi/policy/20thirdparty
if [ -f $synp_fdi_path/$synp_name.old ]; then
	mv -f $synp_fdi_path/$synp_name.old $synp_fdi_path/$synp_name
fi

if [ -f /etc/X11/xorg.conf.old ]; then
	mv /etc/X11/xorg.conf.old  /etc/X11/xorg.conf
fi

if [ -f /etc/X11/xorg.conf.d/11-risintech.conf ]; then
	rm -f /etc/X11/xorg.conf.d/11-risintech.conf 
fi

if [ -f /usr/share/X11/xorg.conf.d/11-risintech.conf ]; then
	rm -f /usr/share/X11/xorg.conf.d/11-risintech.conf 
fi

if [ -f /usr/lib/X11/xorg.conf.d/11-risintech.conf ]; then
	rm -f /usr/lib/X11/xorg.conf.d/11-risintech.conf 
fi

if [ -f /etc/X11/xorg.conf.d/99-risintech.conf ]; then
	rm -f /etc/X11/xorg.conf.d/99-risintech.conf 
fi

if [ -f /usr/share/X11/xorg.conf.d/99-risintech.conf ]; then
	rm -f /usr/share/X11/xorg.conf.d/99-risintech.conf 
fi

if [ -f /usr/lib/X11/xorg.conf.d/99-risintech.conf ]; then
	rm -f /usr/lib/X11/xorg.conf.d/99-risintech.conf 
fi

if [ -f /etc/X11/xorg.conf.d/99-cooltouch.conf ]; then
	rm -f /etc/X11/xorg.conf.d/99-cooltouch.conf 
fi

if [ -f /usr/share/X11/xorg.conf.d/99-cooltouch.conf ]; then
	rm -f /usr/share/X11/xorg.conf.d/99-cooltouch.conf 
fi

if [ -f /usr/lib/X11/xorg.conf.d/99-cooltouch.conf ]; then
	rm -f /usr/lib/X11/xorg.conf.d/99-cooltouch.conf 
fi

if [ -f /usr/share/hal/fdi/policy/10osvendor/10-x11-cooltouch.fdi ]; then
	rm -f /usr/share/hal/fdi/policy/10osvendor/10-x11-cooltouch.fdi
fi

if [ -f /usr/share/hal/fdi/policy/10osvendor/10-x11-risintech.fdi ]; then
	rm -f /usr/share/hal/fdi/policy/10osvendor/10-x11-risintech.fdi
fi

if [ -f /usr/share/hal/fdi/policy/10osvendor/10-x11-input.fdi.old ]; then
	mv /usr/share/hal/fdi/policy/10osvendor/10-x11-risintech.fdi.old /usr/share/hal/fdi/policy/10osvendor/10-x11-risintech.fdi
fi

if [ -f /etc/profile.old ]; then
	mv /etc/profile.old /etc/profile
fi

if [ -f /etc/X11/xinit/xinitrc.d/99-setscreen.sh ]; then
	rm -f /etc/X11/xinit/xinitrc.d/99-setscreen.sh
fi

if [ -f /etc/X11/xinit/xinitrc.d/99-setscreen-def.sh ]; then
	rm -f /etc/X11/xinit/xinitrc.d/99-setscreen-def.sh
fi

if [ -f /etc/init.d/pctservice.sh ]; then
	rm -f /etc/init.d/pctservice.sh
	unlink /etc/rcS.d/S99pctservice
fi

if [ -f /usr/local/bin/setpdrv.sh ]; then
	rm -f /usr/local/bin/setpdrv.sh
fi

if [ -f /usr/share/wayland-sessions/setpdrv.desktop ]; then
	rm -f /usr/share/wayland-sessions/setpdrv.desktop
fi
				
if [ -f /lib/systemd/user/setpdrv.service ]; then
	rm -f /lib/systemd/user/setpdrv.service
fi

if [ -f /etc/systemd/user/setpdrv.service ]; then
	rm -f /etc/systemd/user/setpdrv.service
fi

if [ -f /etc/tpinit ]; then
	rm -f /etc/tpinit
fi

if [ -f /etc/tpxorg ]; then
	rm -f /etc/tpxorg
fi

if [ -f /etc/.pctbackup ]; then
	rm -f /etc/.pctbackup
fi

if [ -f /etc/.restpbin ]; then
	rm -f /etc/.restpbin
fi

if [ -f /etc/.pctpbin ]; then
	rm -f /etc/.pctpbin
fi

if [ -f /etc/.pctpbin ]; then
	rm -f /etc/tpdef.conf
fi

if [ -f /etc/TouchUtil.ini ]; then
	rm -f /etc/TouchUtil.ini
fi

# delete binaries 

for app_name in setscreen setxorg chgxorg Touchpack LinearAp setparams setpctparam setpdrv setevdev Align_Touch touchtool closedev FreeDraw touch_utility setdev tpservice tpsndsev touchpackservice Touch_Alignment debug_tool touch_default touchsndservice touchattach
do
	if [ -f $BIN_DIR/$app_name ]; then
		rm -f $BIN_DIR/$app_name*
	fi
done

if [ -d /usr/local/share/touchtool/ ]; then
	rm -rf /usr/local/share/touchtool/
fi

if [ -f /usr/share/applications/touchtool.desktop ]; then
	rm /usr/share/applications/touchtool.desktop
fi

if [ -f /etc/sertp.conf ]; then
	rm -f /etc/sertp.conf
fi

rm -f /etc/systemd/system/touchattach*
rm -f /lib/systemd/system/touchattach*

if [ -d /opt/.touch_panel/ ]; then
	rm -rf /opt/.touch_panel/
fi

if [ -f /usr/lib/systemd/system-sleep/touch-sleep.sh ]; then
	rm -f /usr/lib/systemd/system-sleep/touch-sleep.sh
fi

if [ -f /etc/gdm/Init/Default.old ]; then
	mv /etc/gdm/Init/Default.old /etc/gdm/Init/Default
fi

if [ -f /etc/modprobe.d/blacklist.conf.old ]; then
	mv /etc/modprobe.d/blacklist.conf.old /etc/modprobe.d/blacklist.conf
fi

if [ -f /etc/modules.old ]; then
	mv /etc/modules.old /etc/modules
fi

if [ -f /etc/modules-load.d/pcspkr.conf ]; then
	rm -f /etc/modules-load.d/pcspkr.conf
fi

if [ -f /etc/modprobe.d/beep.conf.old ]; then
	mv /etc/modprobe.d/beep.conf.old /etc/modprobe.d/beep.conf
fi

if [ -f /etc/sysconfig/modules/pcspkr.modules ]; then
	rm -f /etc/sysconfig/modules/pcspkr.modules
fi

if [ -f ~/.bashrc.old ]; then
	mv ~/.bashrc.old ~/.bashrc
fi

if [ -f /usr/share/gdm/greeter/autostart/setpdrv.desktop ]; then
	rm -f /usr/share/gdm/greeter/autostart/setpdrv.desktop
fi

if [ -f /usr/share/gdm/autostart/LoginWindow/setpdrv.desktop ]; then
	rm -f /usr/share/gdm/autostart/LoginWindow/setpdrv.desktop
fi

if [ -f /etc/xdg/autostart/setpdrv-wayland.desktop ]; then
	rm -f /etc/xdg/autostart/setpdrv-wayland.desktop
fi

if [ -f /usr/share/lightdm/lightdm.conf.d/50-setpdrv.conf ]; then
	rm -f /usr/share/lightdm/lightdm.conf.d/50-setpdrv.conf
fi

if [ -f /etc/xdg/autostart/setpdrv.desktop ]; then
	rm -f /etc/xdg/autostart/setpdrv.desktop
fi

if [ -f /etc/xdg/autostart/cooltouch-xhost.desktop ]; then
	rm -f /etc/xdg/autostart/cooltouch-xhost.desktop
fi

if [ -f /etc/lightdm/setpdrv.sh ]; then
	rm -f /etc/lightdm/setpdrv.sh
fi

if [ -f /etc/lightdm/lightdm.conf.old ]; then
	mv /etc/lightdm/lightdm.conf.old /etc/lightdm/lightdm.conf
fi

if [ -f /usr/share/sddm/Xsetup.old ]; then
	mv /usr/share/sddm/Xsetup.old /usr/share/sddm/Xsetup
fi

if [ -f /etc/kde4/kdm/Xsetup.old ]; then
	mv /etc/kde4/kdm/Xsetup.old /etc/kde4/kdm/Xsetup
fi

if [ -f /etc/kde/kdm/Xsetup.old ]; then
	mv /etc/kde/kdm/Xsetup.old /etc/kde/kdm/Xsetup
fi

if [ -f /etc/selinux/config.old ]; then
	mv /etc/selinux/config.old /etc/selinux/config
fi

for LIB_DIR in /usr/lib/ /usr/lib64/
do
	if [ -f $LIB_DIR/libtouchapi.so.1.0 ]; then
		rm -f $LIB_DIR/libtouchapi.so.1.0
		rm -f $LIB_DIR/libtouchapi.so
		rm -f $LIB_DIR/libtouchapi.so.1
	fi
	
	if [ -f $LIB_DIR/libLinear.so.1.0 ]; then
		rm -f $LIB_DIR/libLinear.so.1.0
		rm -f $LIB_DIR/libLinear.so
		rm -f $LIB_DIR/libLinear.so.1
	fi
	
	if [ -f $LIB_DIR/librisintp.so.1.0 ]; then
		rm -f $LIB_DIR/librisintp.so.1.0
		rm -f $LIB_DIR/librisintp.so
		rm -f $LIB_DIR/librisintp.so.1
	fi
	
	if [ -f $LIB_DIR/libtpinfo.so.1.0 ]; then
		rm -f $LIB_DIR/libtpinfo.so.1.0
		rm -f $LIB_DIR/libtpinfo.so
		rm -f $LIB_DIR/libtpinfo.so.1
	fi
	
	if [ -f $LIB_DIR/libtpdrv.so.1.0 ]; then
		rm -f $LIB_DIR/libtpdrv.so.1.0
		rm -f $LIB_DIR/libtpdrv.so
		rm -f $LIB_DIR/libtpdrv.so.1
	fi

	if [ -f $LIB_DIR/libPct.so.1.0 ]; then
		rm -f $LIB_DIR/libPct.so.1.0
		rm -f $LIB_DIR/libPct.so
		rm -f $LIB_DIR/libPct.so.1
	fi	
done

if [ -f /usr/lib/xorg/modules/input/xfhiddrv_drv.so ]; then
	rm -f /usr/lib/xorg/modules/input/xfhiddrv_drv.so
fi

if [ -f /usr/lib64/xorg/modules/input/xfhiddrv_drv.so ]; then
	rm -f /usr/lib64/xorg/modules/input/xfhiddrv_drv.so
fi

if [ -f /usr/lib/xorg/modules/input/risintech_drv.so ]; then
	rm -f /usr/lib/xorg/modules/input/risintech_drv.so
fi

if [ -f /usr/lib64/xorg/modules/input/risintech_drv.so ]; then
	rm -f /usr/lib64/xorg/modules/input/risintech_drv.so
fi

if [ -f /usr/lib/xorg/modules/input/revdev_drv.so ]; then
	rm -f /usr/lib/xorg/modules/input/revdev_drv.so
fi

if [ -f /usr/lib64/xorg/modules/input/revdev_drv.so ]; then
	rm -f /usr/lib64/xorg/modules/input/revdev_drv.so
fi

if [ -f /etc/udev/hwdb.d/99-risintech-touchpanel.hwdb ]; then
	rm -f /etc/udev/hwdb.d/99-risintech-touchpanel.hwdb
fi

if [ -f /lib/udev/hwdb.d/99-risintech-touchpanel.hwdb ]; then
	rm -f /lib/udev/hwdb.d/99-risintech-touchpanel.hwdb
fi

if [ -f /etc/udev/hwdb.d/99-cooltouch.hwdb ]; then
	rm -f /etc/udev/hwdb.d/99-cooltouch.hwdb
fi

if [ -f /lib/udev/hwdb.d/99-cooltouch.hwdb ]; then
	rm -f /lib/udev/hwdb.d/99-cooltouch.hwdb
fi

if [ -f /usr/local/bin/bcomp ]; then
	rm -f /usr/local/bin/bcomp
fi

echo "Remove completed"

if [ $boot = "1" ]; then
	reboot
fi
